//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// Object labels handling.
//

# include "StdAfx.h"
# include "Map.h"
# include "Types.h"
# include "Label.h"
# include "Globals.h"

bool Is6bitLabelCorrect (const char * _str) {
	for (const char * p = _str; * p; ++ p)
		if (::strchr (" abcdefghijklmnopqrstuvwxyz0123456789@!\"#$%&'()*+,-./:;<=>?[\\]^_`", ::tolower (* p)) == NULL) {
			if (
				p [0] == '~' &&
				p [1] == '[' &&
				p [2] == '0' &&
				(p [3] == 'x' || p [3] == 'X') &&
				::isxdigit (p [4]) &&
				::isxdigit (p [5]) &&
				(
					p [6] == ']' ||
					(
						::isxdigit (p [6]) &&
						::isxdigit (p [7]) &&
						p [8] == ']'
					)
				)
			)
				continue;
			return false;
		}
	return true;
}

bool IsPhoneCorrect (const char * _str) {
	for (const char * p = _str; * p; ++ p)
		if (::strchr ("0123456789 -", * p) == NULL)
			return false;
	return true;
}

void GetHintText (const rgn_t & _rgn, string_t & _strText) {
	const type_desc_t * const pTD = _rgn.GetTypeDesc ();
	assert (pTD);
	if (! pTD)
		return;

	const char * strTypeName = pTD->strTypeName;
	if (pTD->bUnknown) {
		if (_rgn.IsWaypoint ())
			strTypeName = "Waypoint";
		else if (_rgn.IsTrack ())
			strTypeName = "Track";
		else if (_rgn.IsRoute ())
			strTypeName = "Route";
	}

	if (! _rgn.strLabel.empty ()) {
		// TODO: handle such strings as well.
		if (_rgn.strLabel.size () >= 4 && ::_strnicmp (_rgn.strLabel.c_str (), "~[0x", 4) == 0) {
			if (_rgn.strLabel.size () > 6 && _rgn.strLabel [6] == ']')
				_strText.append (_rgn.strLabel.c_str () + 7);
			else if (_rgn.strLabel.size () > 8 && _rgn.strLabel [8] == ']')
				_strText.append (_rgn.strLabel.c_str () + 9);
			else
				_strText.append (_rgn.strLabel);
		} else
			_strText.append (_rgn.strLabel);
		_strText.append (" (");
		_strText.append (strTypeName);
		_strText.append (")");
	} else
		_strText.append (strTypeName);
}
